<template>
  <div class="home">
    <!-- 轮播图 -->
    <div class="carousel">
      <div class="carousel-inner" :style="{ transform: `translateX(-${currentSlide * 100}%)` }">
        <div v-for="(slide, index) in slides" :key="index" class="carousel-slide">
          <img :src="slide.image" :alt="slide.title" @error="handleImageError">
        </div>
      </div>
      <div class="carousel-indicators">
        <button v-for="(slide, index) in slides" 
                :key="index"
                :class="{ active: currentSlide === index }"
                @click="setSlide(index)">
        </button>
      </div>
    </div>

    <!-- 登录状态和按钮 -->
    <div class="auth-section">
      <div v-if="isAuthenticated" class="user-info">
        <span>欢迎回来, {{ username }}</span>
        <button @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
      <router-link v-else to="/login" class="login-btn">登录 / 注册</router-link>
    </div>

    <!-- 最喜欢的装备展示 -->
    <div class="favorite-weapons">
      <h2>我的收藏</h2>
      <div v-if="recommendedWeapons.length === 0" class="no-recommendations">
        还没有收藏的军火，请在军火一览页面标记收藏
      </div>
      <div v-else class="weapons-grid">
        <div v-for="weapon in recommendedWeapons" :key="weapon.id" class="weapon-card">
          <img :src="weapon.image" :alt="weapon.name" @error="handleImageError">
          <h3>{{ weapon.name }}</h3>
          <p>{{ weapon.type }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
  name: 'Home',
  setup() {
    const store = useStore()
    const router = useRouter()
    const currentSlide = ref(0)
    
    const slides = [
      { image: '/slides/slide1.jpg', title: '最新更新' },
      { image: '/slides/slide2.jpg', title: '新皮肤系列' },
      { image: '/slides/slide3.jpg', title: '游戏活动' },
      { image: '/slides/slide4.jpg', title: '比赛资讯' }
    ]

    const isAuthenticated = computed(() => store.state.auth.isAuthenticated)
    const username = computed(() => store.state.auth.user?.username)

    const recommendedWeapons = computed(() => {
      return store.getters.recommendedWeapons
    })

    const handleLogout = () => {
      store.dispatch('auth/logout')
      router.push('/')
    }

    const setSlide = (index) => {
      currentSlide.value = index
    }

    const handleImageError = (e) => {
      e.target.src = '/placeholder.png' // 设置一个默认的占位图片
    }

    // 自动轮播
    let intervalId = null
    const startAutoSlide = () => {
      intervalId = setInterval(() => {
        currentSlide.value = (currentSlide.value + 1) % slides.length
      }, 5000)
    }

    // 组件挂载时启动自动轮播
    onMounted(() => {
      startAutoSlide()
      // 检查登录状态
      store.dispatch('auth/checkAuth')
    })

    // 组件卸载时清除定时器
    onUnmounted(() => {
      if (intervalId) {
        clearInterval(intervalId)
      }
    })

    return {
      slides,
      recommendedWeapons,
      currentSlide,
      setSlide,
      handleImageError,
      isAuthenticated,
      username,
      handleLogout
    }
  }
}
</script>

<style scoped>
.home {
  padding-top: 80px;
}

.carousel {
  width: 90%;  /* 减小宽度，两边留空 */
  max-width: 900px;  /* 设置最大宽度 */
  margin: 0 auto;  /* 水平居中 */
  position: relative;
  overflow: hidden;
  margin-bottom: 2rem;
  aspect-ratio: 2/1;  /* 设置纵横比为 3:4 */
}

.carousel-inner {
  display: flex;
  transition: transform 0.5s ease-in-out;
  height: 100%;
  width: 100%;
}

.carousel-slide {
  min-width: 100%;
  height: 100%;
}

.carousel-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
}

.carousel-indicators button {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: none;
  background-color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
}

.carousel-indicators button.active {
  background-color: #ff4655;
}

.favorite-weapons {
  padding: 2rem;
  max-width: 1400px;  /* 限制最大宽度 */
  margin: 0 auto;
}

.favorite-weapons h2 {
  text-align: center;
  margin-bottom: 2rem;
  color: #ff4655;
}

.weapons-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 2rem;
  padding: 1rem;
  max-width: 1400px;  /* 限制最大宽度，保持与装备总览页面一致 */
}

.weapon-card {
  background-color: #1f1f1f;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s;
}

.weapon-card:hover {
  transform: translateY(-5px);
}

.weapon-card img {
  width: 100%;
  height: 200px;  /* 固定图片高度 */
  object-fit: cover;
}

.weapon-card h3 {
  color: white;
  padding: 1rem;
  margin: 0;
}

.weapon-card p {
  color: #ff4655;
  padding: 0 1rem 1rem;
  margin: 0;
}

.no-recommendations {
  text-align: center;
  padding: 2rem;
  color: #666;
  font-style: italic;
}

.auth-section {
  text-align: center;
  margin: 2rem 0;
  padding: 1rem;
}

.login-btn {
  display: inline-block;
  padding: 0.75rem 2rem;
  background-color: #ff4655;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  font-weight: 600;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #e6323f;
}

.user-info {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  color: white;
}

.logout-btn {
  padding: 0.5rem 1rem;
  background-color: transparent;
  border: 1px solid #ff4655;
  color: #ff4655;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.logout-btn:hover {
  background-color: #ff4655;
  color: white;
}
</style> 